// pages/user/user.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rightIcon: '/images/icon-right.png',
    head: '/images/head.png',
    userInfo: [],
    isSettingIcon: true,
  },


  onShow() {
    this.getUserInfo();
  },
  /**
   * 用户退出登录
   */
  outLogin: function () {

    var that = this;
    app.ajax({
      url: 'Partner/User/log_out',
      method: "POST",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          wx.removeStorageSync('token');
          wx.removeStorageSync('role');
          wx.removeStorageSync('openid');
          wx.removeStorageSync('userInfo');
          app.showToast('退出成功', "success");
          setTimeout(function () {
            wx.redirectTo({
              url: '../../login/login',
            })
          }, 1000);
        } else {
          app.showToast(res.data.message)
        }

      }
    })


  },


  settingIconLoad(e) {
    this.setData({
      settingIconLoad: true,
    })
  },
  settingIconError(e) {
    this.setData({
      settingIconLoad: false,
    })
  },
  //跳转到实名认证
  navAuthentic() {
    wx.navigateTo({
      url: '/package/pages/authentication/entrance/entrance',
    })
  },

  //去设置
  goSet(e) {
    wx.navigateTo({
      url: '../installInfo/installInfo?set_type=' + e.currentTarget.dataset.set_type + '&&info=' + e.currentTarget.dataset.info,
    })

  },
  //跳转设置
  navSetting() {
    wx.navigateTo({
      url: '../install/install',
    })
  },
  //跳转更新记录
  navVersion() {
    wx.navigateTo({
      url: '../version/list/list',
    })
  },
  //选择照片方式
  chooseimage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从手机相册选择', '拍照'],
      itemColor: "black",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.changePic('album')
          } else if (res.tapIndex == 1) {
            that.changePic('camera')
          }
        }
      }
    })

  },
  //修改头像
  changePic(type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success(res) {
        console.info(res.tempFiles);
        // tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        var url = app.globalData._url;

        var token = wx.getStorageSync('token');
        wx.uploadFile({
          url: url + 'Partner/User/wapUserInfoUpdate', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          header: {
            'token': token
          },
          success(res) {
            var data = res.data;
            console.log(res);
            //do something
          }
        })
      }
    })
  },
  getUserInfo() {
    var that = this;
    app.ajax({
      url: 'Partner/User/getPartnerInfo',
      data: {},
      success: function (res) {

        if (res.data.code == 200) {
          that.setData({
            userInfo: res.data.data,
          })

        }
      },

    })
  }
})